#!/bin/sh
# - test
# - load

## AIM7 is a program written in C that forks many processes called
## tasks, each of which concurrently runs in random order a set of
## subtests called jobs. There are 53 kinds of jobs, each of which
## exercises a different aspect of the operating system, such as
## disk-file operations, process creation, user virtual memory
## operations, pipe I/O, and compute-bound arithmetic loops.

[ -n "$test" ] || exit
[ -n "$load" ] || exit

. $LKP_SRC/lib/reproduce-log.sh

aim7_setup()
{
	# raise semaphore limit
	log_eval 'echo "500 32000 128 512" > /proc/sys/kernel/sem'

	# setup workfile
	if [ -f "workfiles/workfile.$test" ]; then
		log_cmd cp workfiles/workfile.$test workfile
	else
		# create workfile onlive
		log_eval "cat > workfile <<EOF
FILESIZE: 1M
POOLSIZE: 10M
10 $test
EOF"
	fi

	# setup config file
	if [ -n "$mount_points" ]; then
		log_eval "echo \"$mount_points\" > config"
	else
		log_cmd mkdir -p /fs/shm
		log_cmd umount /fs/shm > /dev/null 2>&1
		log_cmd mount -t tmpfs /dev/shm /fs/shm
		log_eval 'echo "/fs/shm" > config'
	fi
}

run_aim()
{
	export PATH=$(pwd):$PATH

	aim7_setup

	log_eval "
	(
		echo $HOSTNAME
		echo $test

		echo 1
		echo $load
		echo 2
		echo $load
		echo 1
	) | ./multitask -t &"

	# Run multitask in background and wait for it to avoid the below error
	# triggered by the kill(0, SIGINT) call in multitask.c
	#
	# root /lkp/wfg# src/bin/run-local -o rt aim.yaml
	# ...
	# nanosleep({10, 0}, 0x7fff559ced20)      = 0
	# kill(0, SIGINT <unfinished ...>
	# aim7 pid 55756 exit 130
	# /lkp/wfg/src/bin/run-job: Interrupt
	#
	# AIM Multiuser Benchmark - Suite VII Testing over....
	#
	wait
}

cd $BENCHMARK_ROOT/aim7 || die "aim7 benchmark is not installed yet"

run_aim
